package cn.dkdoo.utils.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    /**
     * 获取所有待修改的文件的绝对路径
     *
     * @param folderPath
     * @return
     * @throws FileNotFoundException
     */
    public static List<String> getAllFile(String folderPath) {
        File directory = new File(folderPath);
        List<String> resPath = new ArrayList<>();
        if (!directory.isDirectory()) {
            throw new RuntimeException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");
        }
        if (directory.isDirectory()) {
            File[] filelist = directory.listFiles();
            for (int i = 0; i < filelist.length; i++) {
                String filePath = filelist[i].getAbsolutePath();
                /**如果当前是文件夹，进入递归扫描文件夹**/
                if (filelist[i].isDirectory()) {
                    /**递归扫描下面的文件夹**/
                    resPath.addAll(getAllFile(filePath));
                } else {
                    /**非文件夹**/
                    if (filePath.indexOf(".java") != -1 && filePath.indexOf("/src/main/java/com/gsst/blackhorse") != -1) {
                        resPath.add(filePath);
                    }
                }
            }
        }
        return resPath;
    }

    public static String readFileToStr(String filePath) {
        int len = 0;
        StringBuffer str = new StringBuffer("");
        File file = new File(filePath);
        try {
            FileInputStream is = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(isr);
            String line = null;
            while ((line = in.readLine()) != null) {
                // 处理换行符的问题
                if (len != 0) {
                    str.append("\r\n" + line);
                } else {
                    str.append(line);
                }
                len++;
            }
            in.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();

    }


    public static void coverOldFile(String filePath, List<String> resList) {
        StringBuilder res = new StringBuilder();
        for (String str : resList) {
            res.append(str).append("\r\n");
        }
        coverOldFile(filePath, res.toString());
    }

    public static void coverOldFile(String filePath, String[] resList) {
        StringBuilder res = new StringBuilder();
        for (String str : resList) {
            res.append(str).append("\r\n");
        }
        coverOldFile(filePath, res.toString());
    }

    public static void coverOldFile(String filePath, String res) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
            out.write(res);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getFileStrHasLine(String filePath) {
        String fileStr = FileUtils.readFileToStr(filePath);
        fileStr = fileStr.replace("\r\n", "##start--end## ");
        int lineNum = 1;
        StringBuilder sb = new StringBuilder();
        for (String str : fileStr.split("##start--end##")) {
            sb.append(str).append("##start-" + lineNum + "-end## ");
            lineNum++;
        }
        return sb.toString();
    }


    public static List<String> toListByFileReader(String name) {
        // 使用ArrayList来存储每行读取到的字符串
        List<String> arrayList = new ArrayList<>();
        try {
            FileReader fr = new FileReader(name);
            BufferedReader bf = new BufferedReader(fr);
            String str;
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
            bf.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public static String toPretty(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.PrettyFormat,
                SerializerFeature.WriteDateUseDateFormat);
    }

    public static String[] toArrayByFileReader(String name) {
        // 使用ArrayList来存储每行读取到的字符串
        List<String> arrayList = toListByFileReader(name);
        return arrayList.toArray(new String[arrayList.size()]);
    }


}
